package com.wy.panda.controller;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wy.panda.common.model.R;
import com.wy.panda.common.page.PageResult;
import com.wy.panda.controller.po.BasePO.CreateGroup;
import com.wy.panda.controller.po.BasePO.UpdateGroup;
import com.wy.panda.controller.po.SysUserPO;
import com.wy.panda.controller.query.BaseQuery.QueryGroup;
import com.wy.panda.controller.query.SysUserQuery;
import com.wy.panda.controller.vo.SysUserVO;
import com.wy.panda.dao.criteria.SysUserCriteria;
import com.wy.panda.entity.SysUserEntity;
import com.wy.panda.service.SysUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

/**
 * 用户
 *
 * @author wangyuan
 * @since 2021-03-27 13:27:02
 */
@Api(value = "用户", tags = "用户")
@Validated
@AllArgsConstructor
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {

    private final SysUserService sysUserService;
    
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/list")
    public R<PageResult<SysUserVO>> list(@Validated(QueryGroup.class) @ModelAttribute SysUserQuery query) {
        PageResult<SysUserEntity> result = sysUserService.listPage(query.convert(SysUserCriteria.class));
        return R.ok(result.convert(SysUserEntity.class, SysUserVO.class));
    }
    
    @ApiOperation(value = "详情")
    @GetMapping(value = "/info/{id}")
    public R<SysUserVO> info(@ApiParam("用户id") @NotNull(message = "参数id不能为空") @PathVariable("id") Long id) {
        SysUserEntity entity = sysUserService.getById(id);
        if(null == entity) {
            return R.error("查询不到记录");
        }
        return R.ok(entity.convert(SysUserVO.class));
    }
    
    @ApiOperation(value = "保存")
    @PostMapping(value = "/save")
    public R<?> save(@Validated(CreateGroup.class) @ModelAttribute SysUserPO po) {
        SysUserEntity entity = po.convert(SysUserEntity.class);
        sysUserService.save(entity);
        return R.ok();
    }
    
    @ApiOperation(value = "更新")
    @PostMapping(value = "/update")
    public R<?> update(@Validated(UpdateGroup.class) @ModelAttribute SysUserPO po) {
        SysUserEntity oldEntity = sysUserService.getById(po.getId());
        if(null == oldEntity) {
            return R.error("更新失败，记录不存在。");
        }
        
        mapperFacade.map(po, oldEntity);
        
        sysUserService.updateById(oldEntity);
        
        return R.ok();
    }
    
    @ApiOperation(value = "（批量）删除")
    @PostMapping(value = "/delete/{ids}")
    public R<?> deleteBatch(@ApiParam("id，多个使用,分隔") @NotNull(message = "参数ids不能为空") @PathVariable List<Long> ids) {
        sysUserService.removeByIds(ids);
        return R.ok();
    }
    
}